/**
* 使用nodejs搭建一个简单的静态资源服务器
*
*/
var http = require('http');    
var url = require('url');   
var fs = require('fs'); //fs  
var server = http.createServer(function (req, res) {  
    var pathname = url.parse(req.url).pathname;  
    var realPath = "resources" + pathname;//所有文件都存在与resources目录下  
    console.log(realPath);  
    fs.exists(realPath, function (exists) {//判断文件是否存在  
        if (!exists) {   
            res.write("404\nNot Found!\n");    
            res.end();    
        } else {  
            fs.readFile(realPath, "binary", function (err, file) {  
                    res.writeHead(200, {"Content-Type":"text/plain"});  
                    res.write(file, "binary");  
                    res.end();  
            });  
        }  
    });  
});  
  
server.listen(8088);  
console.log("静态资源服务器已启动");  